//
//  RHMePlantInfoChartCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/7.
//

#import "RHMePlantInfoChartCell.h"
#import "RHMePlantInfoLineChartView.h"
#import "RHMePlantInfoBarChartView.h"
#import "DevChartModel.h"

@interface RHMePlantInfoChartCell ()
@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) RHMePlantInfoLineChartView *lineChartView;

@property (nonatomic ,strong) RHMePlantInfoBarChartView *barChartView;
@end
@implementation RHMePlantInfoChartCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}


- (void)setChartModel:(DevChartModel *)chartModel{
    _chartModel = chartModel;

    [_lineChartView setChartModel:_chartModel];
    [_barChartView setChartModel:_chartModel];
    self.titleLab.text = chartModel.title;
}

- (void)setupUI{
    self.backgroundColor = UIColor.clearColor;
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView.layer setCornerRadius:8];
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderColor = kCOLOR_HEX(@"#E3E6ED").CGColor;
    bgView.layer.borderWidth = 1;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    //------------------------
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.text = @"能量统计".rh_localized;
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.textAlignment = NSTextAlignmentRight;
    titleLab.font = kBOLDSYSTEMFONT(16);
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
 
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    //日 月  年
    NSInteger type = 0;
    if ([self.reuseIdentifier containsString:@"Month"]) {
        type = 1;
    }
    //soc(只有日曲线)
    if ([self.reuseIdentifier containsString:@"RHMePlantInfoChartCell_lineChart_day"]) {
        type = 2;
    }
    if ([self.reuseIdentifier containsString:@"Year"]) {
        type = 0;
    }
    if ([self.reuseIdentifier isEqualToString:@"RHMePlantInfoChartCell_lineChart"]||
        [self.reuseIdentifier containsString:@"RHMePlantInfoChartCell_lineChart_day"]) {
        _lineChartView = [[RHMePlantInfoLineChartView alloc] initWithFrame:CGRectMake(5, 5,SCREEN_WIDTH-40, 250+130) type:type isLandscape:NO];
        _lineChartView.layer.cornerRadius = 8;
        _lineChartView.layer.masksToBounds = YES;
        _lineChartView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:_lineChartView];
        kWeakSelf;
        _lineChartView.fullChartBlock = ^{
            if (weakSelf.fullChartBlock) {
                weakSelf.fullChartBlock();
            }
        };
        _lineChartView.selectTopToolBlock = ^(NSInteger index) {
            if (weakSelf.selectTopToolBlock) {
                weakSelf.selectTopToolBlock(index);
            }
        };
        
        _lineChartView.subTimeBlock = ^{//日期往前
            if (weakSelf.subTimeBlock) {
                weakSelf.subTimeBlock();
            }
        };
        
        _lineChartView.addTimeBlock = ^{//日期往后
            if (weakSelf.addTimeBlock) {
                weakSelf.addTimeBlock();
            }
        };
        
        _lineChartView.changeTimeBlock = ^{
            if (weakSelf.changeTimeBlock) {
                weakSelf.changeTimeBlock();
            }
        };
        
        
        [_lineChartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(5);
            make.right.mas_offset(-5);
            make.top.equalTo(titleLab.mas_bottom).offset(0);
            make.bottom.mas_offset(0);
        
        }];
    }else{
        _barChartView = [[RHMePlantInfoBarChartView alloc] initWithFrame:CGRectMake(5, 5,SCREEN_WIDTH-40, 250+130) type:type isLandscape:NO];
        _barChartView.layer.cornerRadius = 8;
        _barChartView.layer.masksToBounds = YES;
        _barChartView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:_barChartView];
        kWeakSelf;
        _barChartView.fullChartBlock = ^{
            if (weakSelf.fullChartBlock) {
                weakSelf.fullChartBlock();
            }
        };
        _barChartView.selectTopToolBlock = ^(NSInteger index) {
            if (weakSelf.selectTopToolBlock) {
                weakSelf.selectTopToolBlock(index);
            }
        };
        
        _barChartView.subTimeBlock = ^{//日期往前
            if (weakSelf.subTimeBlock) {
                weakSelf.subTimeBlock();
            }
        };
        
        _barChartView.addTimeBlock = ^{//日期往后
            if (weakSelf.addTimeBlock) {
                weakSelf.addTimeBlock();
            }
        };
        
        _barChartView.changeTimeBlock = ^{
            if (weakSelf.changeTimeBlock) {
                weakSelf.changeTimeBlock();
            }
        };
        
        
        [_barChartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(5);
            make.right.mas_offset(-5);
            make.top.equalTo(titleLab.mas_bottom).offset(0);
            make.bottom.mas_offset(0);
        
        }];
    }
    
}

-(void)dealloc
{
    
}
@end
